<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="投资时间区间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column label="订单ID" prop="id" v-if="columns[0].visible"/>
          <el-table-column label="用户名" prop="username"   v-if="columns[1].visible"/>
          <el-table-column label="交易数量" prop="quantity"  v-if="columns[2].visible"/>
          <el-table-column label="违约金" prop="liquidatedDamages"  v-if="columns[3].visible"/>
          <el-table-column label="算力" prop="power"  v-if="columns[4].visible"/>
          <el-table-column label="订单状态" prop="status" :formatter="statusFormat"  v-if="columns[5].visible"/>
          <el-table-column label="投资天数" prop="investDay" v-if="columns[6].visible"/>
          <el-table-column label="投资时间" width="180" align="center" prop="createTime"  v-if="columns[7].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="结束时间" width="180" align="center" prop="endTime"  v-if="columns[8].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.endTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { orePoolList} from "@/api/biz";

export default {
  name: "OrePool",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: undefined,
        status: undefined
      },
      statusOptions:[
        {
          dictValue:'0',
          dictLabel:'准备中'
        },
        {
          dictValue:'1',
          dictLabel:'挖矿中'
        },
        {
          dictValue:'2',
          dictLabel:'已撤销'
        },
        {
          dictValue:'3',
          dictLabel:'已完成'
        },
        {
          dictValue:'4',
          dictLabel:'已违约'
        }
      ],
      // 列信息
      columns: [
        { key: 0, label: `订单ID`, visible: true },
        { key: 1, label: `用户名`, visible: true },
        { key: 2, label: `交易数量`, visible: true },
        { key: 3, label: `违约金`, visible: true },
        { key: 4, label: `算力`, visible: true },
        { key: 5, label: `订单状态`, visible: true },
        { key: 6, label: `投资时间`, visible: true },
        { key: 7, label: `投资时间`, visible: true },
        { key: 8, label: `结束时间`, visible: true }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      orePoolList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
